package com.tencreat.service.mall.impl;

import com.baomidou.lock.annotation.Lock4j;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.domain.model.LoginUser;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.domain.mall.MallAddress;
import com.tencreat.domain.mall.dto.MallAddressDTO;
import com.tencreat.mapper.mall.MallAddressMapper;
import com.tencreat.service.mall.IMallAddressService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 收货地址Service业务层处理
 *
 * @author tencreat
 * @date 2021-08-13
 */
@Service
@RequiredArgsConstructor
public class MallAddressServiceImpl extends ServiceImpl<MallAddressMapper, MallAddress> implements IMallAddressService {

    /**
     * 查询收货地址
     *
     * @param id 收货地址ID
     * @return 收货地址
     */
    @Override
    public MallAddress selectMallAddressById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询收货地址列表
     *
     * @param mallAddress 收货地址
     * @return 收货地址
     */
    @Override
    public List<MallAddress> selectMallAddressList(MallAddress mallAddress) {
        return baseMapper.selectList(Wrappers.query(mallAddress));
    }

    /**
     * 新增收货地址
     *
     * @param mallAddress 收货地址
     * @return 结果
     */
    @Override
    public int insertMallAddress(MallAddress mallAddress) {
        mallAddress.setCreateTime(LocalDateTime.now());
        return baseMapper.insert(mallAddress);
    }

    @Override
    public int insertDefMallAddress(ReqIdParam reqIdParam, LoginUser loginUser) {
        MallAddress address = lambdaQuery().eq(MallAddress::getUuid, loginUser.getAccountLogin().getUuid()).eq(MallAddress::getIsDefault, 1).one();
        if (address != null) {
            address.setIsDefault(0);
            baseMapper.updateById(address);
        }
        MallAddress mallAddress = lambdaQuery().eq(MallAddress::getUuid, loginUser.getAccountLogin().getUuid()).eq(MallAddress::getId, reqIdParam.getId()).one();
        mallAddress.setIsDefault(1);
        return baseMapper.updateById(mallAddress);
    }

    /**
     * 修改收货地址
     *
     * @param mallAddressDTO 收货地址
     * @return 结果
     */
    @Override
    public int updateMallAddress(MallAddressDTO mallAddressDTO) {
        MallAddress mallAddress = new MallAddress();
        BeanUtils.copyProperties(mallAddressDTO, mallAddress);
        mallAddress.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(mallAddress);
    }

    /**
     * 批量删除收货地址
     *
     * @param ids 需要删除的收货地址ID
     * @return 结果
     */
    @Override
    public int deleteMallAddressByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除收货地址信息
     *
     * @param id 收货地址ID
     * @return 结果
     */
    @Override
    public int deleteMallAddressById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }

    @Override
    public BigDecimal getPostAgeByGood(Long goodId, Integer addressId) {
        return baseMapper.getPostAgeByGood(goodId, addressId);
    }

    @Lock4j(keys = "#uuid")
    @Override
    public void addAddress(MallAddressDTO mallAddressDTO, Long uuid) {
        MallAddress mallAddress = new MallAddress();

        if (mallAddressDTO.getIsDefault() == 1) {
            MallAddress address = lambdaQuery()
                    .eq(MallAddress::getUuid, uuid).eq(MallAddress::getIsDefault, 1)
                    .one();
            if (address != null) {
                address.setIsDefault(0);
                address.setUpdateTime(LocalDateTime.now());
                updateById(address);
            }
        }
        mallAddressDTO.setUuid(uuid);

        BeanUtils.copyProperties(mallAddressDTO, mallAddress);
        mallAddress.setCreateBy("");
        mallAddress.setCreateTime(LocalDateTime.now());
        mallAddress.setUpdateTime(LocalDateTime.now());
        save(mallAddress);
    }
}
